package cn.itsource.config;

import cn.hutool.core.util.IdUtil;
import cn.itsource.constants.RConst;
import cn.itsource.mapper.LoginMapper;
import cn.itsource.pojo.domain.Login;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class BootstrapListener  implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private LoginMapper loginMapper;
	@Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Login login = new Login();
        login.setId(IdUtil.createSnowflake(RConst.Num.INT_1,RConst.Num.INT_1).nextId());
        login.setUsername("admin");
        //加密
        String encode = passwordEncoder.encode("123456");
        login.setPassword(encode);
        login.setType(RConst.Num.INT_0);
        login.setEnabled(true);
        login.setAvatar("https://img0.baidu.com/it/u=4260823727,3010062655&fm=253&fmt=auto&app=120&f=JPEG?w=475&h=475");
        login.setAdmin(true);
        login.setNickName("admin");
        login.setName("admin");
        Login one = loginMapper.selectOne(new LambdaQueryWrapper<Login>()
                .eq(Login::getUsername, login.getUsername())
                .eq(Login::getType, login.getType())
                .eq(Login::getAdmin, login.getAdmin()));
        if (one == null) {
            loginMapper.insert(login);
        }

    }
}